﻿/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.api.common.msg
{
	import com.ericfeminella.collections.ArrayList;
	import com.ericfeminella.collections.Iterator;

	import com.dg.gaming.api.common.*;
	import nanoxml.*;


	/**
	 * Small communication object for joining an existing table.
	 *
	 * @author Bob Marks
	 * @version Alpha 0.2.3
	 */
	public class MsgJoinTable extends MsgTableMessage
	{

		private var playerList:PlayerList=null;

		private var dgModelXML:XMLElement=null;
		private var username:String=null;


		/**
		 * Constructor which takes an XMLElement.
		 *
		 * @param message
		 */
		public function MsgJoinTable(tableNum:String=null, userid:Number=-1, username:String=null, table:Table=null, message:XMLElement=null)
		{

			initUserId(userid);
			this.username=username;
			setTableNum(tableNum);
			// Set table number manually
			if (table != null)
			{
				setTableNum(table.getTableNum());
				// Set player list
				this.playerList=table.getPlayerList();
				if (table.getModel() != null)
				{
					this.dgModelXML=table.getModel().flatten();
				}
			}
			if (message != null)
				super(message);

			if (message == null)
			{
				return;
			}
			this.username=message.getStringAttribute(User.XML_ATT_USERNAME);
			var children:ArrayList=ArrayList.createArrayList(message.chileds);
			for (var i:Iterator=children.iterator; i.hasNext(); )
			{
				var e:XMLElement=XMLElement(i.next());
				var name:String=e.name;
				if (name == Msg.PLAYER_LIST)
				{
					this.playerList=new PlayerList;
					this.playerList.initXml(e);
				}
				else if (name == Msg.MODEL)
				{
					this.dgModelXML=e;
				}
			}
		}

		/**
		 * Return the player list.
		 *
		 * @return   Player list.
		 */
		public function getPlayerList():PlayerList
		{
			return playerList;
		}

		public function getUsername():String
		{
			return username;
		}

		/**
		 * Return the model state.
		 *
		 * @return
		 */
		public function getModelState():XMLElement
		{
			return dgModelXML;
		}

		/**
		 * Return true if this message contains a model.
		 *
		 * @return
		 */
		public function containsModel():Boolean
		{
			return dgModelXML != null;
		}

		/**
		 * Flatten the String for transmission purposes.
		 *
		 * @see com.dg.gaming.api.common.msg.ITransmittable#flatten()
		 */
		public override function flatten():XMLElement
		{
			var message:XMLElement=super.flattenWithName(Msg.JOIN_TABLE);
			message.setAttribute(User.XML_ATT_USERNAME, username);
			if (playerList != null)
			{
				message.addChild(playerList.flatten());
			}
			if (dgModelXML != null)
			{
				message.addChild(dgModelXML);
			}
			return message;
		}
	}
}